package com.example.yqand4v1_1.controller;

import com.example.yqand4v1_1.beans.MsgInfo;
import com.example.yqand4v1_1.beans.SessionList;
import com.example.yqand4v1_1.common.bean.AjaxResult;
import com.example.yqand4v1_1.dao.MsgInfoEntityMapper;
import com.example.yqand4v1_1.dao.SeesionListEntityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class MsgInfoController {

    @Autowired
    private MsgInfoEntityMapper msgInfoEntityMapper;

    @Autowired
    private SeesionListEntityMapper seesionListEntityMapper;

    // 消息列表
    @GetMapping("/msgList")
    public AjaxResult<?> msgList(@RequestParam Integer sessionId){
        SessionList sessionList = seesionListEntityMapper.selectByPrimaryKey(sessionId);
        if(sessionList == null){
            return AjaxResult.success();
        }
        String fromUserId = sessionList.getUserId();
        String toUserId = sessionList.getToUserId();
        List<MsgInfo> msgInfoList = msgInfoEntityMapper.selectMsgList(fromUserId.toString(),toUserId.toString());
        // 更新消息已读
        msgInfoEntityMapper.msgRead(fromUserId.toString(), toUserId.toString());
        // 更新会话里面的未读消息
        seesionListEntityMapper.delUnReadCount(fromUserId, toUserId);
        return AjaxResult.success(msgInfoList);
    }


}
